<?php
/***
 * 
 * @author GuoLong
 * 2014/12/02
 * 
 */

class item extends Model {
	private $table="xdf_item";
	private $fields=array("");
	public function addItem() {
	}
	public function addItemVideo($item_id,$video_id){
		$sql="INSERT INTO `xdf_item_video` (`item_id`,`video_id`) VALUES ('$item_id','$video_id');";
		return $this->insert($sql);
	}
	
	public function getItemJsonById($item_id){
		$sql = 'SELECT `json` FROM `xdf_cache` WHERE `item_id` = '.$item_id.' ORDER BY `id` DESC LIMIT 1';
		return $this->fetchRow($sql);
	}
	
	public function getItemContentById($item_id){
		$sql = 'SELECT `content` FROM `xdf_item` WHERE `id` ='.$item_id;
		$result = $this->fetchRow($sql);
		if($result){
			return $this->_html2str($result['content']);
		}else{
			return false;
		}
	}
	
	public function getItemById($id){
		$sql = 'SELECT * FROM `xdf_item` WHERE `id`='.$id;
		return $this->fetchRow($sql);
	}
	
	public function getItemWithPicById($id){
		$sql = 'SELECT ';
	}
	
	public function getItemListByStr($mode,$string){
		$strlist = explode('_', $string);
		$totalCount = count($strlist);
		$list = array($totalCount);
		for($i=0;$i<$totalCount;$i++){
			if($mode){
				$item = $this->getItemById($strlist[$i]);
				$list[$i] = array("item_id"=>$strlist[$i],"content"=>$this->_html2str($item['content']));
			}else{
				$list[$i] = array("item_id"=>$strlist[$i],"videoStatue"=>1);
			}
		}
		return $list;
		
	}
	/*sift items from system item_lib or user item_lib
	 * user_id=0,system item_lib
	 * user_id<>0,user item_lib
	 */
	public function siftItem($user_id,$subject_id,$difficulty,$itemtype_id,$knowledge_id,$skill_id,$timestamp,$pagesize,$action){
		$filter=array();
		//set subject_id filter
		if( $subject_id <> "0" ){
			if(is_array($subject_id))
				$filter["subject_id"] = " `sub_id` IN ( " . join(",", $subject_id) . ")";
			else
				$filter["subject_id"] = " `sub_id`= '$subject_id'";
		}else{
			$filter["subject_id"] = " TRUE ";
		}
		//set itemtype filter
		if($itemtype_id <> "0"){
			if(is_array($itemtype_id))
				$filter["itemtype_id"] = " `itemtype_id` IN ( " . join(",", $itemtype_id) . ") ";
			else 
				$filter["itemtype_id"] = " `itemtype_id`= '$itemtype_id' ";
		}else 
			$filter["itemtype_id"] = " TRUE ";
		//set difficulty filter
		if( $difficulty <> "0" ){
			if(is_array($difficulty))
				$filter["difficulty"] = " `difficulty` IN ( " . join(",", $difficulty) . ") ";
			else 
				$filter["difficulty"] = " `difficulty`= '$difficulty' ";
		}else 
			$filter["difficulty"] = " TRUE ";
		//set node filter
		if( $knowledge_id <> "0" || $skill_id <> "0"){
			//exist $knowledge_id <> 0 or  exist $skill_id <> 0
			$filter["node"]=" id IN ( SELECT `item_id` ";
			if( $knowledge_id <> "0"){
				//exist $knowledge_id <> 0
				$filter["node"].="FROM `xdf_node_knowledge` WHERE `knowledge_id` ";
				if(is_array($knowledge_id))
					$filter["node"].=" IN (" . join(",", $knowledge_id) . ") ";
				else 
					$filter["node"].= " = '$knowledge_id' ";
				if( $skill_id<>"0" ){
					$filter["node"].="AND `item_id` IN ( SELECT `item_id` FROM `xdf_node_skill` WHERE `skill_id` ";
					if(is_array($skill_id))
						$filter["node"].= " IN (" . join(",", $skill_id) . ") ";
					else 
						$filter["node"].= "= '$skill_id'";
					$filter["node"].=")";
				}
			}else{
				//only exist $skill_id <> 0
				$filter["node"].="FROM `xdf_node_skill` WHERE `skill_id`";
				if(is_array($skill_id))
					$filter["node"].= " IN (" . join(",", $skill_id) . ")";
				else 
					$filter["node"].= " = '$skill_id'";
			}
			$filter["node"].=")";
		}else{
			$filter["node"] = " TRUE ";
		}
		//set collection filter
		if( $user_id <> "0"){
			$filter["collection"]=" id IN ( SELECT `item_id` FROM `xdf_item_collection` WHERE `user_id`= '$user_id')";
		}
		if($action=='pre'){
			$timestamp=0;
			$filter_preOrMore_sql="   LIMIT  0,$pagesize ";
		}else if($action=='more'){
			$filter_preOrMore_sql="   LIMIT  $timestamp,$pagesize ";
		}
		//add filter to sql
		$sql="SELECT `id` as item_id,`content` as content FROM `xdf_item` WHERE"
				.join(" AND ",$filter)."$filter_preOrMore_sql;";
		$rows=$this->fetchAll($sql);
		if($rows)//将HTML转化为string
		foreach ($rows as $key=> $val){
			$item_content_tmp=strip_tags($val["content"]);
			$order   = array("\r\n", "\n", "\r", "\t");
			$replace = ' ';
			// Processes \r\n's first so they aren't converted twice. 
			$item_content = str_replace($order, $replace, $item_content_tmp);	
			$rows[$key]["content"]=mb_substr($item_content, 0, 40, 'utf-8');
			$rows[$key]["timestamp"]=++$timestamp;
		}
// 		print_r($sql);  
		return $rows;
		
	}
	
	private function _html2str($html){
		$tmp=strip_tags($html);
		$order   = array("\r\n", "\n", "\r");
		$replace = ' ';
		// Processes \r\n's first so they aren't converted twice.
		$str = str_replace($order, $replace, $tmp);
		return mb_substr($str, 0, 40, 'utf-8');
	}
	public function sertchItemBySubjectAndIds($key, $subid = "0",$item_id_array, $pageSize = 10, $status = 1) {
		$filter=array();
		if ($subid != "0") {
			 $filter["subject"]= " sub_id = $subid ";
		}
		if(count($item_id_array)<>0)
			$filter["ids"]=" id IN (".join(",",$item_id_array).") ";
		if ($status == 1) {
		}
		$sql="SELECT `id` as item_id,`content` as content FROM `xdf_item` WHERE 
				 content LIKE \"%$key%\"  AND ".join(" AND ",$filter)." ORDER BY id DESC LIMIT  $pageSize";
		return $this->fetchAll ( $sql );
	}
	public function searchAllBySubject($key, $subid = "0", $pageSize = 10, $status = 1) {
		$subjectFilter="";
		if ($subid != "0") {
			 $subjectFilter= " AND sub_id = $subid ";
		}
		if ($status == 1) {
		}
		$sql="SELECT `id` ,`content` as content FROM `xdf_item` WHERE
				 content LIKE \"%$key%\" $subjectFilter ORDER BY id DESC LIMIT  $pageSize";
		return $this->fetchAll ( $sql );
	}
	//教师端获取自己对题目历史视频
	public function getVideoIdByItem($user_id,$item_id){
		$sql="SELECT iv.video_id,v.title,v.time,v.create_time from `xdf_item_video` iv 
			LEFT JOIN `xdf_video` v ON v.id=iv.video_id 
			WHERE item_id=$item_id AND v.user_id=$user_id  AND iv.visible=1";
		return $this->fetchAll($sql);
	}
	public function isMyVideo($user_id,$video_id){
		$sql="SELECT * FROM `xdf_video` WHERE user_id=$user_id AND id=$video_id;";
		return $this->fetchRow($sql);
	}
	public function delVideoId($user_id,$video_id){
		$sql = "UPDATE `xdf_item_video` SET visible=0 WHERE video_id=$video_id";
		return $this->update($sql); 
	}
	
}
